package org.dromara.xxmj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.demo.utils.ValidateUtil;
import org.dromara.xxmj.domain.CourseScore;
import org.dromara.xxmj.domain.ExamInfo;
import org.dromara.xxmj.domain.dto.GetExamDTO;
import org.dromara.xxmj.domain.vo.*;
import org.dromara.xxmj.mapper.CourseScoreMapper;
import org.dromara.xxmj.mapper.ExamInfomationMapper;
import org.dromara.xxmj.mapper.SimulationInfoMapper;
import org.dromara.xxmj.service.ExamInfomationService;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
public class ExamInfomationServiceImpl implements ExamInfomationService {

    @Resource
    private ExamInfomationMapper examInfomationMapper;

    @Resource
    private CourseScoreMapper courseScoreMapper;

    @Resource
    private SimulationInfoMapper simulationInfoMapper;

    @Override
    public List<GeographyVO> getProvinceList() {
        return examInfomationMapper.getProvinceList();
    }

    @Override
    public List<ExamInfoVO> getExamList(GetExamDTO getExamDTO) {
        ValidateUtil.validateNonNegative(getExamDTO.getGeographyId(), "geographyId");

        LambdaQueryWrapper<ExamInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamInfo::getAffiliationIp,getExamDTO.getGeographyId()).eq(StringUtils.isNotEmpty(getExamDTO.getInformationType()),ExamInfo::getInformationType,getExamDTO.getInformationType());
        return examInfomationMapper.selectVoList(queryWrapper);
    }

    @Override
    public List<GetSimulationVO> simulationList(Integer courseId) {
        ValidateUtil.validateNonNegative(courseId,"课程不能为空");
        return simulationInfoMapper.simulationList(courseId);
    }

    @Override
    public ExamStatVO examStat() {
        Long userId = LoginHelper.getUserId();
        LambdaQueryWrapper<CourseScore> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseScore::getStuId, userId);
        List<CourseScore> courseScores = courseScoreMapper.selectList(queryWrapper);
        ExamStatVO result = new ExamStatVO();

        if (!courseScores.isEmpty()) {
            double totalScore = 0;
            double maxScore = 0;

            for (CourseScore courseScore : courseScores) {
                double score = courseScore.getScore(); // 获取分数
                totalScore += score;
                if (score > maxScore) {
                    maxScore = score;
                }
            }

            double averageScore = totalScore / courseScores.size(); // 计算平均分
            result.setExamCount(courseScores.size());
            result.setAverageScore(averageScore); // 假设 ExamStatVO 有 setAverageScore 方法
            result.setHighScore(maxScore);
        }
        return result;
    }

    @Override
    public List<RecentExamVO> recentExam() {
        Long userId = LoginHelper.getUserId();
        return examInfomationMapper.recentExam(userId);
    }


}
